﻿using UnityEngine;
using System;

namespace MGFramework {
    public class TimeTools {
        /// <summary>
        /// 计算两个时间戳相差的时间差
        /// </summary>
        /// <param name="timestamp1">当前的时间</param>
        /// <param name="timestamp2">之前保存的时间</param>
        /// <returns>TimeSpan类型，ts.Days相隔天数，ts.Hours计算天数后余下小时数，ts.Minutes排除前两者余下分钟数，ts.Seconds排除前三者余下秒数</returns>
        public static TimeSpan GetTimeSpan(long timestamp1, long timestamp2)
        {
            TimeSpan ts = new TimeSpan((timestamp1 - timestamp2) * 10000000);
            return ts;
        }

        /// <summary>
        /// 将时间戳转换为日期
        /// </summary>
        /// <param name="timestamp">需要转换的时间戳 long类型</param>
        /// <returns>DateTime类型的时间</returns>
        public static DateTime ConvertTimestampToDateTime(long timestamp)
        {
            int n = timestamp > 100000000000 ? 10000 : 10000000;
            TimeSpan ts = new TimeSpan(timestamp * n);
            return TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0)).Add(ts);
        }

        /// <summary>
        /// 将时间转换为时间戳
        /// </summary>
        /// <param name="time">Date类型的时间</param>
        /// <param name="isMicrosecond">isMicrosecond为true时返回13位时间戳，为false时返回10位时间戳</param>
        /// <returns>时间戳</returns>
        public static long ConvertDateTimeToTimestamp(System.DateTime time, bool isMicrosecond)
        {
            TimeSpan ts = time - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            if (isMicrosecond)
            {
                return Convert.ToInt64(ts.TotalMilliseconds);
            }
            return Convert.ToInt64(ts.TotalSeconds);
        }

        /// <summary>
        /// 获取当前时间戳
        /// </summary>
        /// <param name="isMicrosecond">isMicrosecond为true时返回13位时间戳，为false时返回10位时间戳</param>
        /// <returns>时间戳</returns>
        public static long GetCurrentTimestamp(bool isMicrosecond)
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            if (isMicrosecond){
                return Convert.ToInt64(ts.TotalMilliseconds);
            }
            return Convert.ToInt64(ts.TotalSeconds);
        }

        /// <summary>
        /// 比较短时间与登陆时间戳相差的时间
        /// </summary>
        /// <param name="todayStamp">今天的时间戳</param>
        /// <param name="time">要比较的短时间（格式20180428）</param>
        /// <returns></returns>
        public static int CompareToDate(long todayStamp, int time) {
            DateTime today = ConvertTimestampToDateTime(todayStamp);
            DateTime dt = DateTime.ParseExact(time.ToString(), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
            Debug.Log(dt.ToString("yyyy/MM/dd HH:mm:ss"));
            TimeSpan ts = today.Subtract(dt);
            return ts.Days;
        }

        /// <summary>
        /// 获取短日期
        /// </summary>
        /// <returns></returns>
        public static int GetShortDate() {
            int short_date = 0;
            short_date = int.Parse(DateTime.UtcNow.Year + "" + (DateTime.UtcNow.Month < 10 ? "0" + DateTime.UtcNow.Month : DateTime.UtcNow.Month + "") + (DateTime.UtcNow.Day < 10 ? "0" + DateTime.UtcNow.Day : DateTime.UtcNow.Day + ""));
            return short_date;
        }
    }
}
